import pygame,time    
def drawing(qipan):
    pygame.draw.rect(screen,(0,0,0),(0,45,300,10),0)
    pygame.draw.rect(screen,(0,0,0),(0,95,300,10),0)
    pygame.draw.rect(screen,(0,0,0),(0,145,300,10),0)
    pygame.draw.rect(screen,(0,0,0),(0,195,300,10),0)
    pygame.draw.rect(screen,(0,0,0),(0,245,300,10),0)
    pygame.draw.rect(screen,(0,0,0),(45,0,10,300),0)
    pygame.draw.rect(screen,(0,0,0),(95,0,10,300),0)
    pygame.draw.rect(screen,(0,0,0),(145,0,10,300),0)
    pygame.draw.rect(screen,(0,0,0),(195,0,10,300),0)
    pygame.draw.rect(screen,(0,0,0),(245,0,10,300),0)
    for i in range(0,6):
        for j in range(0,6):
            if qipan[i][j]==0:
                pass
            if qipan[i][j]==1:
                pygame.draw.rect(screen,(255,0,0),(i*100+25,j*100+25,50,50),0) 
            if qipan[i][j]==2:
                pygame.draw.rect(screen,(0,0,0),(i*100+25,j*100+25,50,50),0) 
    pygame.display.flip()
def red():
    print('red wing')
    time.sleep(2)
    pygame.quit()
def hei2():
    print('black wing')
    time.sleep(2)
    pygame.quit()
def wing(x):
    if x[0][0]==x[0][1]==x[0][2]==1:
        red()
    elif x[0][0]==x[0][1]==x[0][2]==2:
        hei2()
    elif x[1][0]==x[1][1]==x[1][2]==1:
        red()
    elif x[1][0]==x[1][1]==x[1][2]==2:
        hei2()
    elif x[2][0]==x[2][1]==x[2][2]==1:
        red()
        print(3)
    elif x[2][0]==x[2][1]==x[2][2]==2:
        hei2()
    elif x[0][0]==x[1][0]==x[2][0]==1:
        red()
    elif x[0][0]==x[1][0]==x[2][0]==2:
        hei2()
    elif x[0][1]==x[1][1]==x[2][1]==1:
        red()
    elif x[0][1]==x[1][1]==x[2][1]==2:
        hei2()
    elif x[0][2]==x[1][2]==x[2][2]==1:
        red()
    elif x[0][2]==x[1][2]==x[2][2]==2:
        hei2()
    elif x[0][0]==x[1][1]==x[2][2]==1:
        red()
    elif x[0][0]==x[1][1]==x[2][2]==2:
        hei2()
    elif x[2][0]==x[1][1]==x[0][2]==1:
        red()
    elif x[2][0]==x[1][1]==x[0][2]==2:
        hei2()
    elif x[0][0]!=0 and x[0][1]!=0 and x[0][2]!=0 and x[1][0]!=0 and x[1][1]!=0 and x[1][2]!=0 and x[2][0]!=0 and x[2][1]!=0 and x[2][2]!=0:
        print("平局")
        time.sleep(2)
        pygame.quit()
qipan=[[2,2,2,2,2,2]
       [2,2,2,2,2,2],
       [2,2,2,2,2,2],
       [0,0,0,0,0,0],
       [0,0,0,0,0,0],
       [0,1,0,0,1,0]]
n=0
pygame.init()
pygame.display.set_caption("井字棋")
screen=pygame.display.set_mode([300,300])
screen.fill([255,255,255])
drawing(qipan)
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEBUTTONUP:
                if event.button==1:
                    x,y=event.pos
                    x,y=int(x/100),int(y/100)
                    if qipan[x][y]==0:
                        qipan[x][y]+=n%2+1
                        n+=1
                        #print(n%2+1)
                    else:
                        print("NO")
                    drawing(qipan)
                    #print(qipan)
                    wing(qipan)